package com.data.serivce.Impl;


import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;

import org.apache.log4j.Logger;

import com.component.DataServiceUtil;
import com.component.code.CommonCode;
import com.component.code.CommonCode.PropIds;
import com.data.dao.IPlayerDao;
import com.data.dao.base.IBaseDao;
import com.data.model.ConfigRoom;
import com.data.model.PlayerDiankong;
import com.data.model.PlayerGameInfo;
import com.data.model.RecordJiesuanError;
import com.data.model.RecordPropModify;
import com.data.serivce.IPlayerService;
import com.data.spring.BeanFactory;
import com.data.third.DataService;
import com.game.serivce.DeskPlayerService;
import com.game.serivce.PlayerService;
import com.game.serivce.RoomDeskService;


public class PlayerDBService implements IPlayerService{
	private IBaseDao<RecordPropModify, Serializable> recordPropModifyDao;
	private IBaseDao<ConfigRoom, Serializable> configRoomDao;
	private IBaseDao<RecordJiesuanError, Serializable> recordJiesuanErrorDao;
	private IBaseDao<PlayerGameInfo, Serializable> playerGameInfoDao;
	private IBaseDao<PlayerDiankong, Serializable> playerDiankongDao;

	static Logger logger = Logger.getLogger(PlayerDBService.class.getName());

	public IBaseDao<PlayerDiankong, Serializable> getPlayerDiankongDao() {
		return playerDiankongDao;
	}

	public IBaseDao<PlayerGameInfo, Serializable> getPlayerGameInfoDao() {
		return playerGameInfoDao;
	}

	public void setPlayerGameInfoDao(IBaseDao<PlayerGameInfo, Serializable> playerGameInfoDao) {
		this.playerGameInfoDao = playerGameInfoDao;
	}

	public void setPlayerDiankongDao(IBaseDao<PlayerDiankong, Serializable> playerDiankongDao) {
		this.playerDiankongDao = playerDiankongDao;
	}

	public IBaseDao<RecordJiesuanError, Serializable> getRecordJiesuanErrorDao() {
		return recordJiesuanErrorDao;
	}

	public void setRecordJiesuanErrorDao(IBaseDao<RecordJiesuanError, Serializable> recordJiesuanErrorDao) {
		this.recordJiesuanErrorDao = recordJiesuanErrorDao;
	}

	public IBaseDao<ConfigRoom, Serializable> getConfigRoomDao() {
		return configRoomDao;
	}

	public void setConfigRoomDao(IBaseDao<ConfigRoom, Serializable> configRoomDao) {
		this.configRoomDao = configRoomDao;
	}

	public IBaseDao<RecordPropModify, Serializable> getRecordPropModifyDao() {
		return recordPropModifyDao;
	}

	public void setRecordPropModifyDao(IBaseDao<RecordPropModify, Serializable> recordPropModifyDao) {
		this.recordPropModifyDao = recordPropModifyDao;
	}


	public void addJingDianJieSuan(PlayerService playerService, DeskPlayerService deskService, long changeMoney, long taxNum, String showToPlayer, String remark)
			throws Exception {
		Long playerId = Long.valueOf(playerService.getPlayer().getPlayerId());
		// 更新道具信息
		int roomId = deskService.getRoomDeskService().getConfigRoomService().roomNumberForDb;
		int propId = PropIds.TONGQIAN.getValue();
		int changeSource = CommonCode.CHANGESOURCE_TTPE_GAMESETTLEMENT;
		Timestamp t = new Timestamp(System.currentTimeMillis());
		RecordPropModify recordPropModify = new RecordPropModify(playerId, roomId, propId, changeMoney, changeSource, t, showToPlayer, remark, playerService
				.getPlayer().getNickName(), taxNum, deskService.getRoomDeskService().getConfigRoomService().gameNumberForDb, deskService.getRoomDeskService().getConfigDesk().getDeskNum(), 0);
		recordPropModifyDao.save(recordPropModify);
		if (changeMoney != 0) {
			String detail = PropIds.TONGQIAN.getValue() + ":" + changeMoney;
			long money_self = playerService.getPlayer().getMoney();
			playerService.getPlayer().setMoney(money_self + changeMoney);
			if (playerService.getPlayer().getPlayerDiankong() != null) {
				PlayerDiankong playerDiankong = playerService.getPlayer().getPlayerDiankong();
				playerDiankong.setFinishFen(playerDiankong.getFinishFen() + changeMoney);
				BeanFactory.PlayerService.updateDianKongInfo(playerDiankong);
			}
			DataService.updatePlayerPropInfo(playerId, deskService.getRoomDeskService().getConfigRoomService().gameNumberForDb,
					CommonCode.CHANGESOURCE_TTPE_GAMESETTLEMENT, detail);
			// if (!flag) {
			// // 结算失败,写记录
			// RecordJiesuanError recordJiesuanError = new
			// RecordJiesuanError(playerId, detail, t);
			// addRecordJiesuanError(recordJiesuanError);
			// playerService.sendMessages(SocketEnum.infoI,
			// Constant.Tishi_JieSuanYanChi_Fail);
			// deskService.exiting("结算失败");
			// } else {
			// 结算完成 更新玩家道具信息
			// }
		}
	}


	public void saveRecordPropModify(RecordPropModify recordPropModify) {
		recordPropModifyDao.save(recordPropModify);
	}
	
	public void savePlayerGameInfo(PlayerGameInfo playerGameInfo) {
		playerGameInfoDao.save(playerGameInfo);
	}

	public void saveConfigRoom(ConfigRoom configRoom) {
		configRoomDao.save(configRoom);
	}
	

	public List<ConfigRoom> getAllRoomInfoForConfig() {
		try {
			System.out.println("11111---");
			String sql = "from ConfigRoom order by gameId,roomId asc";
			List<ConfigRoom> configRoomTypes = configRoomDao.find(sql);
			System.out.println("configRoomTypes"+configRoomTypes);
			return configRoomTypes;
		} catch (Exception e) {
			System.out.println("2222----");
			e.printStackTrace();
			return null;
		}
	}

	public void addRecordJiesuanError(RecordJiesuanError recordJiesuanError) {
		recordJiesuanErrorDao.save(recordJiesuanError);
	}

	public boolean isHaveAddPropModifyRecordForChangeSourceToday(Long playerId, int changeSource, int propId) {
		String hqlString = "from RecordPropModify where playerID=? and changeSource = ? and propId=? and changeNum > 0 and to_days(now()) - to_days(createTime) = 0";
		List<RecordPropModify> recordPropModifies = recordPropModifyDao.find(hqlString, playerId, changeSource, propId);
		if (recordPropModifies != null && recordPropModifies.size() > 0) {
			return true;
		}
		return false;
	}

	public boolean isHavePropModifyRecordForChangeSource(String playerId, String changeSource, int propId) {
		// String hqlString =
		// "from RecordPropModify where playerID=? and changeSource = ? and propId=? and gameId=?";
		// List<RecordPropModify> recordPropModifies =
		// recordPropModifyDao.find(hqlString, Long.valueOf(playerId),
		// changeSource, propId, Config.GAME_MAHJONG_ID);
		// if (recordPropModifies != null && recordPropModifies.size() > 0) {
		// return true;
		// }
		return false;
	}

	public void updatePropId(PlayerService playerService, DeskPlayerService deskService, String showToPlayer, long changeMoney, Long playerId,
			int propId) {
		// 更新道具信息
		int roomId = deskService.getRoomDeskService().getConfigRoomService().roomNumberForDb;
		int changeSource = CommonCode.CHANGESOURCE_TTPE_GAMESETTLEMENT;
		String remark = "";

		Timestamp t = new Timestamp(System.currentTimeMillis());
		RecordPropModify recordPropModify = new RecordPropModify(playerId, roomId, propId, changeMoney, changeSource, t, showToPlayer, remark, playerService
				.getPlayer().getNickName(), 0l, deskService.getRoomDeskService().getConfigRoomService().gameNumberForDb, deskService.getRoomDeskService().getConfigDesk().getDeskNum(), 0);
		recordPropModifyDao.save(recordPropModify);
		// 更新玩家道具信息
		long money_self = playerService.getPlayer().getMoney();
		if (propId == PropIds.TONGQIAN.getValue()) {
			playerService.getPlayer().setMoney(money_self + changeMoney);
		}
		String detail = propId + ":" + changeMoney;
		boolean flag = DataService.updatePlayerPropInfo(playerId, deskService.getRoomDeskService().getConfigRoomService().gameNumberForDb,
				CommonCode.CHANGESOURCE_TTPE_GAMESETTLEMENT, detail);
		if (!flag) {
			RecordJiesuanError recordJiesuanError = new RecordJiesuanError(playerId, detail, t);
			addRecordJiesuanError(recordJiesuanError);
		}
	}

	public void updatePropId(PlayerService playerService, RoomDeskService roomService, String showToPlayer, long changeMoney, Long playerId,
			int propId) {
		// 更新道具信息
		int roomId = roomService.getConfigRoomService().roomNumberForDb;
		int changeSource = CommonCode.CHANGESOURCE_TTPE_GAMESETTLEMENT;
		String remark = playerService.getPlayer().getNickName() + ":" + 0 + ":" + "-1";

		Timestamp t = new Timestamp(System.currentTimeMillis());
		RecordPropModify recordPropModify = new RecordPropModify(playerId, roomId, propId, changeMoney, changeSource, t, showToPlayer, remark, playerService
				.getPlayer().getNickName(), 0l, roomService.getConfigRoomService().gameNumberForDb, -1, 0);
		recordPropModifyDao.save(recordPropModify);
		// 更新玩家道具信息
		long money_self = playerService.getPlayer().getMoney();
		if (propId == PropIds.TONGQIAN.getValue()) {
			playerService.getPlayer().setMoney(money_self + changeMoney);
		}
		String detail = propId + ":" + changeMoney;
		boolean flag = DataService.updatePlayerPropInfo(playerId, roomService.getConfigRoomService().gameNumberForDb,
				CommonCode.CHANGESOURCE_TTPE_GAMESETTLEMENT, detail);
		if (!flag) {
			RecordJiesuanError recordJiesuanError = new RecordJiesuanError(playerId, detail, t);
			addRecordJiesuanError(recordJiesuanError);
		}
	}

	/** -----------------------------点控--------------------------------- **/

	public void updateDianKongInfo(PlayerDiankong playerDiankong) {
		playerDiankongDao.save(playerDiankong);
	}

	public void deleteDianKongInfoById(Long id) {
		playerDiankongDao.deleteById(id);
	}

	public PlayerDiankong getDianKongInfoById(Long id) {
		return playerDiankongDao.getById(id);
	}

	public List<PlayerDiankong> getAllDianKongInfo(Long playerId) {
		String sql = "from PlayerDiankong where playerId =? and isFinish=? order by createTime asc";
		List<PlayerDiankong> playerDiankongs = playerDiankongDao.find(sql, playerId, 0);
		return playerDiankongs;
	}

	public PlayerDiankong getDianKongInfoLast(Long playerId) {
		String sql = "from PlayerDiankong where playerId =? and isFinish=? order by createTime asc";
		List<PlayerDiankong> playerDiankongs = playerDiankongDao.find(sql, playerId, 0);
		if (playerDiankongs != null && playerDiankongs.size() > 0) {
			return playerDiankongs.get(0);
		} else {
			return null;
		}
	}

	public PlayerGameInfo getGameInfoByPlayerId(Long playerId, int gameId, int roomId) {
		// TODO Auto-generated method stub
		String sql = "from PlayerGameInfo where playerId =? and gameId =? and roomId = ?";
		List<PlayerGameInfo> playerGameInfos = playerGameInfoDao.find(sql, playerId, gameId, roomId);
		if (playerGameInfos != null && playerGameInfos.size() > 0) {
			return playerGameInfos.get(0);
		} else {
			return null;
		}
	}
	/** -----------------------------点控--------------------------------- **/
}
